package com.samp.bblock.core.web;

import javax.servlet.http.HttpServletRequest;

import com.samp.bblock.core.web.util.SupportJsonHttpRequest;
import org.springframework.context.ApplicationContext;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.servlet.DispatcherServlet;

@SuppressWarnings("serial")
public class BblockDispatcherServlet extends DispatcherServlet {
	
	private SupportJsonHttpRequest supportJsonHttpRequest;

	@Override
	protected void initStrategies(ApplicationContext context) {
		super.initStrategies(context);
		initSupportJsonHttpRequest(context);
	}

	@Override
	protected HttpServletRequest checkMultipart(HttpServletRequest request)	throws MultipartException {
		if( this.supportJsonHttpRequest != null && this.supportJsonHttpRequest.support(request) ){
			return this.supportJsonHttpRequest.wrapRequest(request);
		}
		return super.checkMultipart(request);
	}
	
	public void initSupportJsonHttpRequest(ApplicationContext context){
		String[] names = context.getBeanNamesForType(SupportJsonHttpRequest.class);
		if( names != null && names.length > 0 ){
			this.supportJsonHttpRequest = (SupportJsonHttpRequest)context.getBean(names[0]);
		}
	}
}
